<script setup>
import {userLoginByCode} from '@/service/user'
  import { onLaunch, onShow, onError,onLoad } from '@dcloudio/uni-app';
// 微信小程序静默授权登陆
  onLaunch(() => {
    console.log(122);
   const isLogout = uni.getStorageSync('isLogout')
   if(!isLogout){
     login();
     getUserInfo();
     uni.removeStorageSync('isLogout');
   }
    uni.showShareMenu({
      withShareTicket: true,
      path:'pages/index/index',
      success: function (res) {
        console.log(res);
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      },
    });
  });

const login = async () => {
  return new Promise(async (resolve, reject) => {
    // 1. 获得微信 code
    console.log(122);
    const codeResult = await uni.login();
		console.log(codeResult,'codeResult');
    if (codeResult.errMsg !== 'login:ok') {
      return resolve(false);
    }
    // 2. 社交登录
    const loginResult = await userLoginByCode({type:34, code:codeResult.code, state:'default'});
    console.log(loginResult,'loginResultApp');
			uni.setStorageSync('openid', loginResult.openid);
      uni.setStorageSync('accessToken',loginResult.accessToken)
      uni.$emit('login');
      console.log('注册全局登陆');
      return resolve(true);
  });
};
// 获取用户信息
const getUserInfo = async () => {
  return new Promise(async (resolve, reject) => {
    const userInfo = await uni.getStorageSync('userInfo');
    if (userInfo) {
      return resolve(userInfo);
    }
    const userInfoResult = await uni.getUserInfo();
    // uni.setStorageSync('userInfo', userInfoResult.userInfo);
    console.log('userInfo',userInfo);
    return resolve(userInfoResult.userInfo);
  });
};
</script>

<style>
	/*每个页面公共css */
	page {
		background-color: #f7f7f7;
		}
	.box{
		background: #FFFFFF;
		border-radius: 20rpx;
	}
</style>
